// @formatter:off
/**
 * @file c.comic.js
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 19:41:09 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.comic.js 72 2013-06-16 19:41:09Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/themes/default/templates/contents/c.comic.js $
 */

var	readStatus	= <:ISSET:var=CP-READ:/>true<::/><:ISSET:!:var=CP-READ:/>false<::/>;

var	shortcutsButtons	= {
		F:	"#FirstButton",
		C:	"#Previous5Button",
		Z:	"#PreviousButton",
		A:	"#AsTableButton",
		X:	"#NextButton",
		V:	"#Next5Button",
		L:	"#LastButton",

		T:	"#TagButton",
		R:	"#ReadLight"
};
var	shortcutsRating	= {
		"1":	1,
		"2":	2,
		"3":	3,
		"4":	4,
		"5":	5,
		"6":	6,
		"7":	7,
		"8":	8,
		"9":	9,
		"0":	10
};

function handleSliderChange(e, ui) {
	$("#Thumbs").scrollLeft(ui.value);
}
function startShowComic() {
	if (document.getElementById('FullImage')) {
		$('#FullImage').fadeIn('600');
	}
}
function ToogleLight() {
	if(readStatus) {
		$("#ReadLight").attr({
			src:	"<:VAR:/>CP-LIGHT-ON<::/>",
			title:	"Read"
		});
	} else {
		$("#ReadLight").attr({
			src:	"<:VAR:/>CP-LIGHT-OFF<::/>",
			title:	"Unread"
		});
	}
	$("#ReadLight").focus();
}
function ToogleRead() {
	$("#ReadLight").attr({
		src:	"<:VAR:/>CP-LIGHT-LOADING<::/>"
	});

	jQuery.post('service.php?action=<:VAR:/>WC_SERVICE_SETREADRATE<::/>&group=<:VAR:/>GROUP-ID<::/>&comic=<:VAR:/>COMIC-ID<::/>', {
		'READ':		readStatus ? "N" : "Y"
	}, function(data){
		var	xml = $($.parseXML(data));
		if(xml.find("wc-success").length) {
			readStatus = xml.find("wc-result>personal>read").length != 0;
			$("#ReadMessageStatus").text(readStatus ? "read" : "unread");
			TipMessage.open($("#ReadMessage"));
		} else if(xml.find("wc-failed").length) {
			var	error = "";
			if(xml.find("wc-error>wc-code").length) {
				error = "[" + xml.find("wc-error>wc-code").text() + "] ";
			}
			error = error + xml.find("wc-error>wc-message").text();

			alert("Failed: " + error);
		}

		ToogleLight();
	});
}
function ChangeRate(event, value){
	value = value * 2;

	jQuery.post('service.php?action=<:VAR:/>WC_SERVICE_SETREADRATE<::/>&group=<:VAR:/>GROUP-ID<::/>&comic=<:VAR:/>COMIC-ID<::/>', {
		'RATE':		value
	}, function(data){
		var	xml = $($.parseXML(data));
		if(xml.find("wc-success").length) {
			var	rate = xml.find("wc-result>personal>rate").text();
			$("#UserRate").rateit("value", rate/2);
			$("#RateMessageValue").text(rate);
			TipMessage.open($("#RateMessage"));
		} else if(xml.find("wc-failed").length) {
			var	error = "";
			if(xml.find("wc-error>wc-code").length) {
				error = "[" + xml.find("wc-error>wc-code").text() + "] ";
			}
			error = error + xml.find("wc-error>wc-message").text();

			alert("Failed: " + error);
		}
	});
}
function LoadReadRate() {
	$("#ReadLight").attr({
		src:	"<:VAR:/>CP-LIGHT-LOADING<::/>"
	});
	$("#UserRate").rateit("value", 0);

	jQuery.get('service.php?action=<:VAR:/>WC_SERVICE_SETREADRATE<::/>&group=<:VAR:/>GROUP-ID<::/>&comic=<:VAR:/>COMIC-ID<::/>',function(data){
		var	xml = $($.parseXML(data));
		if(xml.find("wc-success").length) {
			var	rate = xml.find("wc-result>personal>rate").text();
			readStatus = xml.find("wc-result>personal>read").length != 0;

			$("#UserRate").rateit("value", rate/2);
			$("#RateMessageValue").text(rate);
			$("#ReadMessageStatus").text(readStatus ? "read" : "unread");

			ToogleLight();
		} else if(xml.find("wc-failed").length) {
			var	error = "";
			if(xml.find("wc-error>wc-code").length) {
				error = "[" + xml.find("wc-error>wc-code").text() + "] ";
			}
			error = error + xml.find("wc-error>wc-message").text();
		}
	});
}
function StartRatings() {
<:ISSET:var=CP-USER-RATE:/>
	$("#UserRate").rateit("max", 5);
	$("#UserRate").bind('over', function(event, value){
		$("#UserRate").attr("title", (value*2)+"/10");
	});
	LoadReadRate();
	$("#UserRate").bind("rated", ChangeRate);
	$("#ReadLight").bind("click", ToogleRead);
<::/>
}
function AddShortCuts() {
	for(var key in shortcutsButtons) {
		WCShortCuts.add(key, function(key, e) {
			$(shortcutsButtons[key]).trigger("click");
		});
	}
	for(var key in shortcutsRating) {
		WCShortCuts.add(key, function(key, e) {
			ChangeRate(e, (shortcutsRating[key] / 2));
			//
			//for(var key in shortcutsRating) {
			//	shortcut.remove(key);
			//}
		});
	}
}
function RemoveShortCuts() {
//	for(var key in shortcutsButtons) {
//		shortcut.remove(key);
//	}
//	for(var key in shortcutsRating) {
//		shortcut.remove(key);
//	}
}
function EditTags() {
//	RemoveShortCuts();
	wcSimpleContainer.openUrl('simplecontainer.php?action=<:VAR:/>WC_SACTION_COMIC_TAGS<::/>&group=<:VAR:/>GROUP-ID<::/>&comic=<:VAR:/>COMIC-ID<::/>', SelfReload);
	return false;
}
function EditName() {
//	RemoveShortCuts();
	wcSimpleContainer.openUrl('simplecontainer.php?action=<:VAR:/>WC_SACTION_COMIC_RENAME<::/>&group=<:VAR:/>GROUP-ID<::/>&comic=<:VAR:/>COMIC-ID<::/>',SelfReload);
	return false;
}

$(document).ready(function() {
	startShowComic();
	AddShortCuts();
	WCShortCuts.start();

	$("input[type=text]").focusin(RemoveShortCuts).focusout(AddShortCuts);
	$("textarea").focusin(RemoveShortCuts).focusout(AddShortCuts);
});

function ShortcutsHelp() {
	wcSimpleContainer.open($("#ShortcutsHelp"),function(){
	});
	wcSimpleContainer.title("Shortcuts Help");
}
